///////////////////////////////////////////////////////////////////
// Student name:Jon Hersack and Caleb Reinking
// Course: COSC 3403
// Assignment: #7 - Initial Software Construction
// File name:Controller.java
// Purpose: Provides logic checking for game functions and manages the
// gameplay progression
// Assumptions:none known
// Limitations:none known
// Development Computer:Intel core2 duo 2.67Ghz
// Operating System: windows
// Integrated Development Environment (IDE): netbeans 6.5
// Compiler: Javac java 1.6.0_13
///////////////////////////////////////////////////////////////////
import java.util.*;

public class Controller
{
    Vector<Team> teamVect;
    Deck mainDeck;//will be singleton
    int activeTeam;
    UserInterface myUserInterface;
    Scanner console = null;

    //contructors
    Controller()
    {
        console = new Scanner(System.in);

        teamVect = new Vector<Team>(2);
        teamVect.add(new Team(1, "Team1"));
        teamVect.add(new Team(0, "Team2"));

        myUserInterface = new UserInterface();

        mainDeck = Deck.getInstanceOf();
        activeTeam = 0;
    }//blank contructor

    public Team getTeam(int teamIndex)
    {
        return teamVect.elementAt(teamIndex);
    }

    public Team getActiveTeam()
    {
        return teamVect.elementAt(activeTeam);
    }

    //#####################################################
    void deal()
    {

    }//end method deal

    //#####################################################
    void newHand()
    {

    }//end method newhand

    //#####################################################
    void startGame()
    {
        myUserInterface.printMainSplash();
        console.nextLine();
        dealToTeams();
        

        while(teamVect.get(0).gameScore < 5000 )
        {
            while(teamVect.get(0).handMileage < 1000 )
            {
                startTurn();
            }
            myUserInterface.printRoundOverSplash(teamVect);
            console.nextLine();
            teamVect.get(activeTeam).handMileage = 0;
            resetBoard();
            dealToTeams();
        }

        myUserInterface.printGameOverSplash(teamVect);
        console.nextLine();

    }//end startGame

    //#####################################################
    void endGame()
    {

    }//end endGame

    //#####################################################
    int tryPlay( Card cardPlayed )
    {
        int otherTeam = (activeTeam+1)%(teamVect.size());
        boolean playValid = false;
              switch(cardPlayed.getTypeID())
        {
            case 1: playValid = getActiveTeam().checkValidDistance(25);
                    break;
            case 2: playValid = getActiveTeam().checkValidDistance(50);
                    break;
            case 3: playValid = getActiveTeam().checkValidDistance(75);
                    break;
            case 4: playValid = getActiveTeam().checkValidDistance(100);
                    break;
            case 5: playValid = getActiveTeam().checkValidDistance(200);
                    break;
                //card goes to distance pile
            case 6:   playValid = teamVect.get(otherTeam).checkValidHazard(cardPlayed.getTypeID());
            case 7:   playValid = teamVect.get(otherTeam).checkValidHazard(cardPlayed.getTypeID());
            case 8:   playValid = teamVect.get(otherTeam).checkValidHazard(cardPlayed.getTypeID());
            case 10:  playValid = teamVect.get(otherTeam).checkValidHazard(cardPlayed.getTypeID());
            // card goes on other team's battle pile
                     break;
            case 9:  playValid = teamVect.get(otherTeam).checkValidHazard(cardPlayed.getTypeID());
            //stop card goes on other team's speed pile
                     break;
            case 11: playValid = getActiveTeam().checkValidRemedy(cardPlayed.getTypeID());
            case 12: playValid = getActiveTeam().checkValidRemedy(cardPlayed.getTypeID());
            case 13: playValid = getActiveTeam().checkValidRemedy(cardPlayed.getTypeID());
            case 15: playValid = getActiveTeam().checkValidRemedy(cardPlayed.getTypeID());
            // card goes on own team's battle pile
                    break;
            case 14: playValid = getActiveTeam().checkValidRemedy(cardPlayed.getTypeID());
            // card goes on own team's speed pile
                     break;
            case 16:
            case 17:
            case 18:
            case 19: playValid = true;
                    break;
           

        }//end switch
       if(playValid)return 1;
       else return -1;
    }//end tryPlay

    //#####################################################
    void doPlay(Card cardPlayed)
    {
        int otherTeam = (activeTeam+1)%(teamVect.size());
        switch( cardPlayed.getTypeID() )
        {
            //if played on active team's pile,
            //then default and playOnPile determines where to play it
            case 6:
            case 7:
            case 8:
            case 10: // card goes on other team's battle pile
                teamVect.get(otherTeam).playOnPile(cardPlayed);
                     break;
            case 9: // card goes on other team's speed pile
                teamVect.get(otherTeam).playOnPile(cardPlayed);
                teamVect.get(otherTeam).hazardStatus[1]=true;//set speedLimit true
                     break;
            default:
                getActiveTeam().playOnPile(cardPlayed);
                     break;
        }//end switch

    }//end doPlay


    //#####################################################
    char promptUser(String question)
    {
        myUserInterface.askUser( question );
        display();
        char answer = console.nextLine().charAt(0);
        myUserInterface.clearQuestion();
       // display();
        return answer;
    }//end promptUser

    //#####################################################
    void parseResponse(String Response)
    {


    }//end promptUser

    //#####################################################
    Card drawCard()
    {
        Card topCard = mainDeck.drawCard();
        if (topCard==null)
        {
            //set delayed action flag so we give bonus points to winning team
        }//end if

        return topCard;
    }//end promptUser

    //#####################################################
    void startTurn()
    {
        //draw a card
        Card topCard = drawCard();
              //check for empty deck
        if(topCard!=null)
        {
            getActiveTeam().giveCard(topCard);
        }//end if
        
        boolean turnOver=false;


        while(!turnOver)
        {
        Character tempChar = promptUser("It's your turn! Which card would you like to play? (1-7):  ");
        Integer tempInt = Integer.parseInt(tempChar.toString());

        if( tempInt == 9 )
        {
            teamVect.get(activeTeam).handMileage += 800;
            teamVect.get(activeTeam).gameScore += 800;
            continue;
        }

        int playable = tryPlay(getActiveTeam().getActivePlayer().getHand().getCard(tempInt-1));

         if(playable==1)
         {
             doPlay(getActiveTeam().getActivePlayer().playCard(tempInt));
             display();
             turnOver=true;
         }//end if

         else
         {
             tempChar = promptUser("Sorry, not a valid play. Would you like to discard instead?(y/n):  ");
             if(tempChar=='y')
             {
                getActiveTeam().getActivePlayer().playCard(tempInt);//removes card from hand
                turnOver = true;
             }//end if

         }//end else
        }//end while
    }//end startturn

    //#####################################################
    void turnCompleted()
    {


    }//end promptUser

    //################################################
    void dealToTeams()
    {
        Vector<Card> tempVector = new Vector<Card>(12);
        for( int i = 0 ; i < 2 ; i++)
        {
            for( int j = 0 ; j < 12 ; j++)
            {
                tempVector.add(mainDeck.drawCard());
            }
            teamVect.elementAt(i).giveHands(tempVector);
            tempVector.clear();
        }
    }

    //##############################################
    void display()
    {
        myUserInterface.display(activeTeam, teamVect);
    }


        //##############################################
    void resetBoard()
    {
        mainDeck.shuffleDeck();
        teamVect.get(0).pileVect.get(0).clearPile();
        teamVect.get(0).pileVect.get(1).clearPile();
        teamVect.get(0).resetTeam();
        teamVect.get(1).resetTeam();
        teamVect.get(1).pileVect.get(0).clearPile();
        teamVect.get(1).pileVect.get(1).clearPile();

    }//resetBoard


    //##############################################
    public static void main( String args[] )
    {//I copied this to the millebornes.main method in compliance with
        //the requirements

        Controller myController = new Controller();
        myController.dealToTeams();

        //System.out.println(myController.getTeam(0).toString() + "\n" +
        //                   myController.getTeam(1).toString() );

        myController.display();

    } // end testMain

}//end class Controller
